##Pooled_study_1_and_2_appendix.R
##for appendix, pool studies to check interactions with greater power
##R version 3.6.1 


##################################################################
##                 Prepare Study 1                              ##
##################################################################
##set working directory for data
setwd("...")  ##SET WORKING DIRECTORY HERE

library(haven)

data1<- read_dta("Agenda_Clean_SSI_v4.dta")
names(data1)

#################################
##Rename variables to match study 2

data1$answer_main<- ifelse(!is.na(data1$o_cong) & !is.na(data1$o_bill), 1, 0)
summary(as.factor(data1$answer_main))

foo<- lm(o_bill ~ as.factor(full_treatment) + as.factor(policy), data=data1, subset=c(data1$answer_main==1))
summary(foo) ##0 is control, 1 is bipartisan, 2 is minority 
summary(as.factor(data1$full_treatment))

data1$treatment_bipart<- ifelse(data1$full_treatment==1, 1, 0)
data1$treatment_min<- ifelse(data1$full_treatment==2, 1, 0)
summary(as.factor(data1$treatment_bipart))
summary(as.factor(data1$treatment_min))

foo2<- lm(o_bill ~ as.factor(full_treatment)*as.factor(majrel) + as.factor(policy), data=data1, subset=c(data1$answer_main==1 & data1$ispureind==0))
summary(foo2) ##majrel = 2 if minority vote 
summary(as.factor(data1$majrel))

data1$maj_other<- ifelse(data1$majrel==2, 1, 0)
summary(as.factor(data1$maj_other))

data1$policy_sent<- ifelse(data1$policy==2, 1, 0)
summary(as.factor(data1$policy))
summary(as.factor(data1$policy_sent))

##DVs
data1$bill<- data1$o_bill
data1$cong<- data1$o_cong
data1$ft_maj<- data1$o_warm_maj_01

foo3<- lm(bill ~ treatment_bipart*maj_other + treatment_min*maj_other + policy_sent, data=data1, subset=c(data1$answer_main==1 & data1$ispureind==0))
summary(foo3)

##independents
xtabs(~data1$ispureind + data1$pid3)
summary(as.factor(data1$pid3)) ##3 is pure ind
data1$pid3_combine<- ifelse(data1$pid3==3, "Ind", "Not Ind")
summary(as.factor(data1$pid3_combine))

##study 1 indicator
data1$study_number<- 1


##################################################################
##                 Prepare Study 2                              ##
##################################################################
library(foreign)

data2<- read.dta("Study_2_TESS_for_analysis.dta", convert.factors=FALSE)

##Prepare data - only need to change independents
data2$pid3_combine<- ifelse(data2$pid3=="Ind", "Ind", "Not Ind")
summary(as.factor(data2$pid3_combine))

##study 2 indicator
data2$study_number<- 2

##################################################################
##       Combine relevant DV and IVs                            ##
##################################################################
library(dplyr)

data1.relevant<- select(data1, study_number, pid3_combine, answer_main, cong, bill, ft_maj, treatment_bipart, 
                        treatment_min, maj_other)
data2.relevant<- select(data2, study_number, pid3_combine, answer_main, cong, bill, ft_maj, treatment_bipart, 
                        treatment_min, maj_other)
data.pool.relevant<- as.data.frame(rbind(data1.relevant, data2.relevant))
names(data.pool.relevant)

##################################################################
##                 Regressions                                  ##
##################################################################
##with interaction
##bill evaluation
reg.pool.bill<- lm(bill ~ treatment_bipart*maj_other + treatment_min*maj_other + as.factor(study_number),
                   data=data.pool.relevant,
                   subset=c(data.pool.relevant$pid3_combine!="Ind" & data.pool.relevant$answer_main==1))
summary(reg.pool.bill) 

##congress
reg.pool.cong<- lm(cong ~ treatment_bipart*maj_other + treatment_min*maj_other + as.factor(study_number),
               data=data.pool.relevant,
               subset=c(data.pool.relevant$pid3_combine!="Ind" & data.pool.relevant$answer_main==1))
summary(reg.pool.cong) 

##feelings toward majority
reg.pool.maj<- lm(ft_maj ~ treatment_bipart*maj_other + treatment_min*maj_other + as.factor(study_number),
              data=data.pool.relevant,
              subset=c(data.pool.relevant$pid3_combine!="Ind" & data.pool.relevant$answer_main==1))
summary(reg.pool.maj) 


###############################
##Output regressions
source("regtable.R")

##working directory for output
setwd(".../output")  ##SET WORKING DIRECTORY HERE

##Pooled models, interaction - appendix
outtable.rtf(list("(1) Bill"=reg.pool.bill, "(2) Congress"=reg.pool.cong, "(3) Majority"=reg.pool.maj),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("treatment_bipart", "Ignore Bipartisan Alternative"),
                              c("treatment_min", "Ignore Minority Alternative"),
                              c("maj_other", "Minority Voters"),
                              c("treatment_bipart:maj_other", "Ign. Bipartisan X Min. Voters"),
                              c("maj_other:treatment_min", "Ign. Minority X Min. Voters"),
                              c("as.factor(study_number)2", "Study 2")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Appendix Table F1.rtf")
